<?php

include('include/config.inc.php');


function get_web_page( $url )
{
    $options = array(
        CURLOPT_RETURNTRANSFER => true,     // return web page
        CURLOPT_HEADER         => false,    // don't return headers
        CURLOPT_FOLLOWLOCATION => true,     // follow redirects
        CURLOPT_ENCODING       => "",       // handle all encodings
        CURLOPT_USERAGENT      => "ChangeBot Spider, Columbian College Dean's Office", // who am i
        CURLOPT_AUTOREFERER    => true,     // set referer on redirect
        CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
        CURLOPT_TIMEOUT        => 120,      // timeout on response
        CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
    );

    $ch      = curl_init( $url );
    curl_setopt_array( $ch, $options );
    $content = curl_exec( $ch );
    $err     = curl_errno( $ch );
    $errmsg  = curl_error( $ch );
    $header  = curl_getinfo( $ch );
    curl_close( $ch );

    $header['errno']   = $err;
    $header['errmsg']  = $errmsg;
    $header['content'] = $content;
    return $header;
}



function hash_page( $url, $strip_comments = 0, $strip_images = 0, $return_cleanpage = 0){
	
	global $hash_algo;

	$page = get_web_page($url);

	$page_content = $page['content'];

	if($strip_comments == '1'){
		$page_content = preg_replace('/<!--(.*)-->/Uis', '', $page_content);
	}

	if($strip_images == '1'){
		$page_content = preg_replace("/<img[^>]+\>/i", " ", $page_content); 
	}


	$hash = hash($hash_algo, $page_content);

	if($return_cleanpage == 1){
		return $page_content;}
	else{
		return $hash;}
	

}


function record_hash ( $url_id, $algo, $current_hash, $give_new_record_id = 0){


			
			$record_hash_query = "INSERT INTO `changebot`.`history` (`record_id`, `url_id`, `time`, `algo`, `hash`, `config`) VALUES (NULL, '".$url_id."', '".time()."', '".$algo."', '".$current_hash."', '')";

			mysql_query($record_hash_query);

			if($give_new_record_id == '1'){
				return mysql_insert_id();
			}

}



function last_known_hash( $url, $id = 0 ){

			$last_hash = "SELECT record_id,time,algo,hash,urls.url_id,url FROM `history` LEFT JOIN `urls` on history.url_id = urls.url_id WHERE `url` = '".$url."' ORDER BY time DESC LIMIT 0,1";

			$last_hash_resource = mysql_fetch_array(mysql_query($last_hash));

			if($id == '1'){
				return $last_hash_resource['record_id'];
			}
			else{
				return $last_hash_resource['hash'];
			}



}


function compare_verbose($astr, $bstr){

	/* this function graciously provided by Christopher Dugan, Northwestern U
	on this day, the twenty eigth day of the month of September, in the year of our Lord
	two thousand and ten.  */

	$count = 0;
	$aa = array();
	$bb = array();
	while (strlen($astr) != $count){
		$aa[] = substr($astr, $count, 1);
		$bb[] = substr($bstr, $count, 1);
		$count++;
		}
	$count = 0;
	$hit = -1;
	foreach($aa as $a){
		$b = $bb[$count];
		if ($a !== $b){
			$hit = $count;
			}
		$count++;
		}

	if($hit == -1){
		return true;
	}
	else{
		return false;
	}
	

}


function add_log_entry( $prechange_hash_record, $new_record_id, $url, $type  ) {

				$changelog_entry = "INSERT INTO `log` (`log_id`, `log_timestamp`, `lower_bound`, `upper_bound`, `type`, `url_id`) VALUES (NULL, '".time()."', '".$prechange_hash_record."', '".$new_record_id."', '".$type."', '".$url."')";


				mysql_query($changelog_entry);

}




?>